package com.lu.luapiinterface.controller;


import com.lu.luapiinterface.Utils.SignUtils;
import com.lu.luapiinterface.entity.User;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.Instant;

@RestController
@RequestMapping("/name")
public class NameController {

    @GetMapping("/")
    public  String getName(String name ){

        return "GET请求:"+ name;
    }

    @PostMapping("/")
    public  String postName(@RequestParam String name){
        return "POST请求:"+ name;
    }

    @PostMapping("/user")
    public  String jsonName(@RequestBody User user, HttpServletRequest request){
        String accessKey = request.getHeader("accessKey");
        String nonce = request.getHeader("nonce");
        String timestamp = request.getHeader("timestamp");
        String sign = request.getHeader("sign");
        String body = request.getHeader("body");

        //accessKey
        if (!accessKey .equals("abc")){
            throw  new RuntimeException("无权限！！");
        }
        //nonce
        if (Long.parseLong(nonce) >10000){
            throw  new RuntimeException("无权限！！");
        }
        String serverSign = SignUtils.getSign(body, "abc");
        if (!sign.equals(serverSign)){
            throw  new RuntimeException("无权限！！");
        }
        return "JSON:"+ user.getUsername();
    }

}
